package bairui.international.weixin.portal.miniapp.config;

import java.io.File;
import java.time.Duration;

import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.Ssl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.lianjing.sh.project.config.base.profile.Project_Profile;
import com.lianjing.sh.spring.web.controller.enhance.EnableControllerEnhance;

import lombok.Getter;

@Configuration
@Import({
	Project_Profile.class,
	Config_WebMvcConfiguration.class
})
@EnableControllerEnhance
public class Config_Web {

	@Bean
	public TomcatServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
			@Override
			protected void postProcessContext(Context context) {
				SecurityCollection collection = new SecurityCollection();
					collection.addPattern("/*");
				//
				SecurityConstraint securityConstraint = new SecurityConstraint();
					securityConstraint.setUserConstraint("CONFIDENTIAL");
					securityConstraint.addCollection(collection);
				//
				context
					.addConstraint(securityConstraint);
			}
		};
		//
	    Ssl ssl = new Ssl();
	        ssl.setKeyStore("classpath:bairui.tetong.cc.pfx");
	        ssl.setKeyStorePassword("uve5yrd5");
    	tomcat.setSsl(ssl);
//    	tomcat.setPort(server_port);
    	tomcat.getSession().setTimeout(Duration.ofMinutes(30));
    	tomcat.setBaseDirectory(new File("/tomcatTemp/tomcat-" + tomcat.getPort()));
		//
		return tomcat;
	}

}
